import AsyncStorage from '@react-native-async-storage/async-storage'
import { makeAutoObservable } from 'mobx'
const userInfoInit = {
  id: 0,
  name: '',
  phone: '',
  cover: '',
  account: '',
  focusNum: 0,
  friendsNum: 0,
}
type userInfo = typeof userInfoInit
type loginParam = {
  password?: string
  phone: string
  code?: string
  type: 'code' | 'password' | 'register'
}
class User {
  userInfo: typeof userInfoInit = userInfoInit
  constructor() {
    makeAutoObservable(this)
  }
  get getId() {
    return this.userInfo.id
  }
  SET_USER_INFO(userInfo: userInfo) {
    this.userInfo = userInfo
    AsyncStorage.setItem('userInfo', JSON.stringify(userInfo))
  }
  logout() {
    this.SET_USER_INFO(userInfoInit)
  }
  async login(obj: loginParam) {
    if (!obj.phone) {
      return '手机号码不能为空'
    }

    if (obj.type === 'code') {
      if (!obj.code) return '验证码不能为空'
      if (obj.phone !== '16620498141') {
        return '请输入正确的电话号码'
      }
      if (obj.code !== '000000') {
        return '请输入正确的验证码'
      }
    } else {
      if (!obj.password) return '密码不能为空'
      if (obj.phone !== '16620498141') {
        return '请输入正确的电话号码'
      }
      if (obj.password !== 'Aa123456.') {
        return '密码不正确'
      }
    }
    await new Promise(res => {
      setTimeout(() => {
        this.SET_USER_INFO({
          id: 1,
          name: 'Adam',
          phone: '16620498141',
          cover: '',
          account: '绿叶晨阳',
          focusNum: 0,
          friendsNum: 0,
        })
        res(0)
      }, 2000)
    })
    return ''
  }
}
const user = new User()
export default user
